package com.eascs.admin.o2o.detail.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.eascs.admin.common.service.BaseSevice;
import com.eascs.admin.o2o.back.service.O2oBackService;
import com.eascs.admin.o2o.detail.form.LoanDetailButtonForm;
import com.eascs.admin.o2o.finance.entity.O2oFinanceFlowEntity;
import com.eascs.admin.o2o.finance.service.O2oFinanceFlowService;
import com.eascs.admin.o2o.internal.dao.LoanapplyExpandDao;
import com.eascs.admin.o2o.internal.entity.LoanapplyExpand;
import com.eascs.admin.o2o.internal.util.StatusContexts;
import com.eascs.admin.o2o.loan.entity.O2oLoanUserEntity;
import com.eascs.admin.o2o.loan.entity.O2oLoanapplyEntity;
import com.eascs.admin.o2o.loan.service.O2oLoanUserService;
import com.eascs.admin.o2o.loan.service.O2oLoanapplyService;
import com.eascs.admin.o2o.loan.utils.BankRate;
import com.eascs.admin.o2o.o2ozx.serviceimpl.O2oZxServiceImpl;
import com.eascs.admin.o2o.specialInterview.entity.O2OSpecialInterview;
import com.eascs.admin.o2o.specialInterview.service.O2OSpecialInterviewService;
import com.eascs.admin.o2o.zx.entity.O2oZxEntity;
import com.eascs.afterloan.entity.AdvancedPayment;
import com.eascs.afterloan.service.AdvanceSettleService;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.pay.entity.O2oPayEntity;
import com.eascs.pay.service.impl.O2oPayServiceImpl;
import com.eascs.site.view.button.Button;
import com.eascs.site.view.button.ButtonHelper;
import com.eascs.web.sso.User;
import com.eascs.web.sso.UserHolder;

@Service
public class LoanDetailButtonService extends BaseSevice {
	private static Logger log = LoggerFactory.getLogger(LoanDetailButtonService.class);
	@Autowired
	private LoanapplyExpandDao loanapplyExpandDao;
	@Autowired
	private O2oLoanapplyService o2oloanapplyService;
	@Autowired
	private O2oPayServiceImpl o2oPayService;
	@Autowired
	private O2oFinanceFlowService financeFlowService;
	@Autowired
	AdvanceSettleService advanceSettleService;
	@Autowired
	O2OSpecialInterviewService o2OSpecialInterviewService;
	@Autowired
	private O2oLoanUserService o2oLoanUserService;
	@Autowired
	O2oZxServiceImpl o2oZxServiceImpl;
	@Autowired
	private O2oBackService o2oBackService;
	public List<Button> buildButtons(LoanDetailButtonForm loanDetailButtonForm) {
		List<Button> buttons = new ArrayList<>();
		O2oLoanapplyEntity o2oloanapply = o2oloanapplyService.findById(loanDetailButtonForm.getLoanapplyNo());
		if (StatusContexts.O2OLIKETYPE_TYPE_ZX.equals(loanDetailButtonForm.getO2oliketype())) {
			
			if (StatusContexts.O2O_NEW.equals(loanDetailButtonForm.getWfType())) {
				buttons.add(ButtonHelper.createOpenButton("编辑征信准入信息", "/loanapply/findloan?loanapplyNo=" + loanDetailButtonForm.getLoanapplyNo(), "loanapplyUpdate"));
				if (loanDetailButtonForm.getMaterId() == null) {
					buttons.add(ButtonHelper.createAjaxButton("提交征信准入流程", "/work/zxzrstartup?loanapplyNo=" + loanDetailButtonForm.getLoanapplyNo() + "&billCode=" + loanDetailButtonForm.getBillCode() + "&wfType=" + loanDetailButtonForm.getWfType(), "loanapplyCredit"));
				} else {
					buttons.add(ButtonHelper.createAjaxButton("提交征信准入流程", "/work/zxzrstartup?loanapplyNo=" + loanDetailButtonForm.getLoanapplyNo() + "&billCode=" + loanDetailButtonForm.getBillCode() + "&wfType=" + loanDetailButtonForm.getWfType() + "&materId=" + loanDetailButtonForm.getMaterId(), "loanapplyCredit"));
				}
				User user = UserHolder.getUser();
				if (StringUtils.isNotBlank(o2oloanapply.getCreatRo()) && o2oloanapply.getCreatRo().equals(user.getNo())) {
					//buttons.add(ButtonHelper.createAjaxButton("作废", "/rbadloan/cacle?loanapplyNo=" + loanDetailButtonForm.getLoanapplyNo() + "&tType=" + StatusContexts.CANCLE_TYPE));
				}
				//buttons.add(ButtonHelper.createAjaxButton("作废", "/rbadloan/cacle?loanapplyNo=" + loanDetailButtonForm.getLoanapplyNo() + "&tType=" + StatusContexts.CANCLE_TYPE, "loanapplyCancel"));
			} else if (StatusContexts.O2O_ZX_BEGIN.equals(loanDetailButtonForm.getWfType())) {
				// 
				if (isStartUpFinance(loanDetailButtonForm.getLoanapplyNo()) && o2oloanapply.getLoanType().equals(StatusContexts.LOAN_TYPE_BK) && StatusContexts.OPERATION_MODEL_PTDB.equals(o2oloanapply.getOperationModel()))
					buttons.add(ButtonHelper.createAjaxButton("提交财务审核", "/work/startup_finance?loanapplyNo=" + loanDetailButtonForm.getLoanapplyNo() + "&billCode=" + loanDetailButtonForm.getBillCode() + "&wfType=" + loanDetailButtonForm.getWfType(), "loanapplyCXsh"));
				//buttons.add(ButtonHelper.createAjaxButton("作废", "/rbadloan/cacle?loanapplyNo=" + loanDetailButtonForm.getLoanapplyNo() + "&tType=" + StatusContexts.CANCLE_TYPE, "loanapplyCancel"));
			} else {// 征信列表，征信完成显示按钮
				if (StatusContexts.O2O_NS_BEGIN.equals(loanDetailButtonForm.getWfType()) || StatusContexts.O2O_NS_END.equals(loanDetailButtonForm.getWfType())) {
					buttons.add(ButtonHelper.createOpenButton("查看最新审核进度", "/oldo2olike/detail_oldlike/ns?isclick=Y&loanapplyNo=" + loanDetailButtonForm.getLoanapplyNo(), "findNsDetailButton"));// ,
																																																	// null);
				}
				if (StatusContexts.O2O_SB_BEGIN.equals(loanDetailButtonForm.getWfType())) {
					buttons.add(ButtonHelper.createOpenButton("查看最新审核进度", "/oldo2olike/detail_oldlike/mqsb?isclick=Y&loanapplyNo=" + loanDetailButtonForm.getLoanapplyNo(), "findNsDetailButton"));// ,
																																																	// null);
				}
			}
		} else if (StatusContexts.O2OLIKETYPE_TYPE_NS.equals(loanDetailButtonForm.getO2oliketype())) {

			if (StatusContexts.O2O_ZX_END.equals(loanDetailButtonForm.getWfType())) {
				if (isStartUpFinance(loanDetailButtonForm.getLoanapplyNo()) && o2oloanapply.getLoanType().equals(StatusContexts.LOAN_TYPE_BK) && StatusContexts.OPERATION_MODEL_PTDB.equals(o2oloanapply.getOperationModel()))
					buttons.add(ButtonHelper.createAjaxButton("提交财务审核", "/work/startup_finance?loanapplyNo=" + loanDetailButtonForm.getLoanapplyNo() + "&billCode=" + loanDetailButtonForm.getBillCode() + "&wfType=" + loanDetailButtonForm.getWfType(), "loanapplyCXsh"));
				buttons.add(ButtonHelper.createOpenButton("编辑内审信息", "/nsEditDetailView/detail?loanapplyNo=" + loanDetailButtonForm.getLoanapplyNo(), "UpdateInternalInfo"));
				buttons.add(ButtonHelper.createAjaxButton("提交内审", "/work/submit?loanapplyNo=" + loanDetailButtonForm.getLoanapplyNo() + "&billCode=" + loanDetailButtonForm.getBillCode() + "&wfType=" + loanDetailButtonForm.getWfType(), "loanapplyInternal"));
				//buttons.add(ButtonHelper.createAjaxButton("作废", "/rbadloan/cacle?loanapplyNo=" + loanDetailButtonForm.getLoanapplyNo() + "&tType=" + StatusContexts.CANCLE_TYPE, "loanapplyCancel"));
			} else if (StatusContexts.O2O_NS_BEGIN.equals(loanDetailButtonForm.getWfType())) {
				if(isStatus(loanDetailButtonForm.getLoanapplyNo()))
					buttons.add(ButtonHelper.createAjaxButton("修改收费金额", "/loanDetail/editExpand?loanapplyNo=" + loanDetailButtonForm.getLoanapplyNo() + "&tType=" + StatusContexts.CANCLE_TYPE, "UpdateloanapplyExpand"));
			} else if (StatusContexts.O2O_NS_END.equals(loanDetailButtonForm.getWfType())) {
				if(isStatus(loanDetailButtonForm.getLoanapplyNo()))
					buttons.add(ButtonHelper.createAjaxButton("修改收费金额", "/loanDetail/editExpand?loanapplyNo=" + loanDetailButtonForm.getLoanapplyNo() + "&tType=" + StatusContexts.CANCLE_TYPE, "UpdateloanapplyExpand"));
			} else {
				if (StatusContexts.O2O_SB_BEGIN.equals(loanDetailButtonForm.getWfType())) {
					if(isStatus(loanDetailButtonForm.getLoanapplyNo()))
						buttons.add(ButtonHelper.createAjaxButton("修改收费金额", "/loanDetail/editExpand?loanapplyNo=" + loanDetailButtonForm.getLoanapplyNo() + "&tType=" + StatusContexts.CANCLE_TYPE, "UpdateloanapplyExpand"));
					buttons.add(ButtonHelper.createOpenButton("查看最新审核进度", "/oldo2olike/detail_oldlike/mqsb?isclick=Y&loanapplyNo=" + loanDetailButtonForm.getLoanapplyNo(), "findNsDetailButton"));// ,
				}
			}
		} else if (StatusContexts.O2OLIKETYPE_TYPE_MQSB.equals(loanDetailButtonForm.getO2oliketype())) {
			if (StatusContexts.O2O_NS_END.equals(loanDetailButtonForm.getWfType())) {
				log.info("按钮逻辑-内审完成:isStatus=" + isStatus(loanDetailButtonForm.getLoanapplyNo()));
//				if (isStatus(loanDetailButtonForm.getLoanapplyNo())) {
//					log.info("按钮逻辑-提交保证金与管理费流程");
//					buttons.add(ButtonHelper.createAjaxButton("提交保证金与管理费流程", "/work/submitBzj?loanapplyNo=" + loanDetailButtonForm.getLoanapplyNo() + "&billCode=" + loanDetailButtonForm.getBillCode() + "&wfType=" + loanDetailButtonForm.getWfType(), "loanapplyBzj"));
//				}
				
				buttons.add(ButtonHelper.createAjaxButton("补充征信报告", "/ro2ozx/supplement_index?loanapplyNo=" + loanDetailButtonForm.getLoanapplyNo(), "loanFqZxbgNew"));
				// 如果是分期贷接力贷，内审完成不出现面签申报按钮
				if (StatusContexts.LOAN_TYPE_BK.equals(o2oloanapply.getLoanType()) || StatusContexts.LOAN_TYPE_BP.equals(o2oloanapply.getLoanType()) || StatusContexts.LOAN_TYPE_BP_JX.equals(o2oloanapply.getLoanType()) || StatusContexts.LOAN_TYPE_SG.equals(o2oloanapply.getLoanType()) || StatusContexts.LOAN_TYPE_YC.equals(o2oloanapply.getLoanType()) || StatusContexts.LOAN_TYPE_SC.equals(o2oloanapply.getLoanType())) {
					buttons.add(ButtonHelper.createOpenButton("下载带数据合同", "/contractDetail?loanapplyNo=" + loanDetailButtonForm.getLoanapplyNo(), "downloadContract"));
					buttons.add(ButtonHelper.createOpenButton("下载O2O金融合作协议", "/protocolDetail?loanapplyNo=" + loanDetailButtonForm.getLoanapplyNo(), "downloadProtocol"));

					int count = o2oloanapplyService.findDCLoan(o2oloanapply.getLoanId());
					if (count > 0) {
						O2OSpecialInterview ishasSpecialInterview = o2OSpecialInterviewService.fingHasFlow(loanDetailButtonForm.getLoanapplyNo());
						if (ishasSpecialInterview == null) { // 如果还没有启动过 （没有run状态的，考虑到驳回了还有可能要提交）
							Button tsmqsb = ButtonHelper.createAjaxButton("特殊面签申报", "/rspecialInterview/save_form?loanapplyNo=" + loanDetailButtonForm.getLoanapplyNo(), "TSMQSP_BUTTON");
							buttons.add(tsmqsb);
						} else if (ishasSpecialInterview.getBillStatus().equals(StatusContexts.O2O_BANK_CLO)) { // 如果这个单存在特殊面签流程，并且还流程中clo显示面签申报按钮
							buttons.add(ButtonHelper.createAjaxButton("面签申报", "/work/submitApproval?loanapplyNo=" + loanDetailButtonForm.getLoanapplyNo() + "&billCode=" + loanDetailButtonForm.getBillCode() + "&wfType=" + loanDetailButtonForm.getWfType(), "loanapplyBankReport"));
						}

					} else {
						buttons.add(ButtonHelper.createAjaxButton("面签申报", "/work/submitApproval?loanapplyNo=" + loanDetailButtonForm.getLoanapplyNo() + "&billCode=" + loanDetailButtonForm.getBillCode() + "&wfType=" + loanDetailButtonForm.getWfType(), "loanapplyBankReport"));
					}
				}
				// 具体的启动逻辑(内审完自动启动的都是直贷流程)
				if (o2oloanapply.getLoanType().equals(StatusContexts.LOAN_TYPE_FQ) || o2oloanapply.getLoanType().equals(StatusContexts.LOAN_TYPE_EA)) {
					// 先去判断这个单是否已经存在直贷流程
					O2oPayEntity o2oPayEntity = new O2oPayEntity();
					o2oPayEntity.setIsdel(StatusContexts.O2O_NO);
					o2oPayEntity.setLoanapplyBillcode(loanDetailButtonForm.getBillCode());
					o2oPayEntity.setBillStatus(StatusContexts.WFTYPE_STATUS_NEW);// 新建状态才能启动流程
					o2oPayEntity.setPayType(StatusContexts.PAY_TYPE_ZD);
					if (!o2oPayService.isHavePayFlow(o2oPayEntity)) {
						buttons.add(ButtonHelper.createAjaxButton("启动付款流程", "/work/startpay?loanapplyNo=" + loanDetailButtonForm.getLoanapplyNo() + "&billCode=" + loanDetailButtonForm.getBillCode() + "&wfType=" + loanDetailButtonForm.getWfType(), "loanapplyBankQdFf"));
					}
				}
				//buttons.add(ButtonHelper.createAjaxButton("作废", "/rbadloan/cacle?loanapplyNo=" + loanDetailButtonForm.getLoanapplyNo() + "&tType=" + StatusContexts.CANCLE_TYPE, "loanapplyCancel"));
			} else if (StatusContexts.O2O_SB_BEGIN.equals(loanDetailButtonForm.getWfType())) {
//				if (isStatus(loanDetailButtonForm.getLoanapplyNo())) {
//					buttons.add(ButtonHelper.createAjaxButton("提交保证金与管理费流程", "/work/submitBzj?loanapplyNo=" + loanDetailButtonForm.getLoanapplyNo() + "&billCode=" + loanDetailButtonForm.getBillCode() + "&wfType=" + loanDetailButtonForm.getWfType(), "loanapplyBzj"));
//				}
				// buttons.add(ButtonHelper.createOpenButton("补充附件",
				// "/payment/otherUpload?referId=" +
				// loanDetailButtonForm.getLoanapplyNo(),
				// "loapplyDetailUpload"));
				//buttons.add(ButtonHelper.createAjaxButton("作废", "/rbadloan/cacle?loanapplyNo=" + loanDetailButtonForm.getLoanapplyNo() + "&tType=" + StatusContexts.CANCLE_TYPE, "loanapplyCancel"));
				O2oLoanUserEntity loanUser = o2oLoanUserService.findById(o2oloanapply.getLoanId(), o2oloanapply.getLoanapplyNo());
				O2oZxEntity o2oZxEntity=new O2oZxEntity();
				if (loanUser!=null) {
					o2oZxEntity=o2oZxServiceImpl.getEntityById(loanUser.getZxNo());
				}
				if ((o2oloanapply.getLoanType().equals(StatusContexts.LOAN_TYPE_BK) || StatusContexts.LOAN_TYPE_BP.equals(o2oloanapply.getLoanType()) || StatusContexts.LOAN_TYPE_BP_JX.equals(o2oloanapply.getLoanType()) || StatusContexts.LOAN_TYPE_SG.equals(o2oloanapply.getLoanType()) || StatusContexts.LOAN_TYPE_YC.equals(o2oloanapply.getLoanType()) || StatusContexts.LOAN_TYPE_SC.equals(o2oloanapply.getLoanType())) && BankRate.BANK_BC.equals(o2oZxEntity.getZxBank())) {
					buttons.add(ButtonHelper.createOpenButton("下载带数据合同", "/contractDetail?loanapplyNo=" + loanDetailButtonForm.getLoanapplyNo(), "downloadContract"));
				}
			} else if (StatusContexts.O2O_FK_END.equals(loanDetailButtonForm.getWfType())) {
//				if (isStatus(loanDetailButtonForm.getLoanapplyNo())) {
//					buttons.add(ButtonHelper.createAjaxButton("提交保证金与管理费流程", "/work/submitBzj?loanapplyNo=" + loanDetailButtonForm.getLoanapplyNo() + "&billCode=" + loanDetailButtonForm.getBillCode() + "&wfType=" + loanDetailButtonForm.getWfType(), "loanapplyBzj"));
//				}
				/*
				 * if (isPlaceOnFile(loanDetailButtonForm.getLoanapplyNo())) {
				 * buttons.add(ButtonHelper.createAjaxButton("启动归档",
				 * "/archive/rest/startArchive?loanapplyNo=" +
				 * loanDetailButtonForm.getLoanapplyNo(), "placeonfile")); }
				 */
				/*
				 * if
				 * (!StatusContexts.LOAN_TYPE_EA.equals(o2oloanapply.getLoanType
				 * ()) &&
				 * !StatusContexts.LOAN_TYPE_FQ.equals(o2oloanapply.getLoanType(
				 * ))) { buttons.add(ButtonHelper.createAjaxButton("还款完成",
				 * "/workflowsubmit/repayment?loanapplyNo=" +
				 * loanDetailButtonForm.getLoanapplyNo(), "repaymentComplete"));
				 * }
				 */
				/*
				 * if (isTqhk(loanDetailButtonForm.getLoanapplyNo())) {
				 * buttons.add(ButtonHelper.createAjaxButton("发起贷款结清",
				 * "/loanDetail/advancedPay?loanapplyNo=" +
				 * loanDetailButtonForm.getLoanapplyNo(),
				 * "advancedRedemption")); }
				 */
			}
			/*
			 * else if
			 * (StatusContexts.O2O_REPAY_END.equals(loanDetailButtonForm.
			 * getWfType())) { if
			 * (isPlaceOnFile(loanDetailButtonForm.getLoanapplyNo())) {
			 * buttons.add(ButtonHelper.createAjaxButton("启动归档",
			 * "/archive/rest/startArchive?loanapplyNo=" +
			 * loanDetailButtonForm.getLoanapplyNo(), "placeonfile")); } } else
			 * if
			 * (StatusContexts.O2O_FK_DC.equals(loanDetailButtonForm.getWfType()
			 * )) { if (isPlaceOnFile(loanDetailButtonForm.getLoanapplyNo())) {
			 * buttons.add(ButtonHelper.createAjaxButton("启动归档",
			 * "/archive/rest/startArchive?loanapplyNo=" +
			 * loanDetailButtonForm.getLoanapplyNo(), "placeonfile")); } if
			 * (isDc(loanDetailButtonForm.getLoanapplyNo())) {
			 * buttons.add(ButtonHelper.createAjaxButton("启动代偿分期",
			 * "/rback/start?loanApplyNo=" +
			 * loanDetailButtonForm.getLoanapplyNo(), "startBack")); } } else if
			 * (StatusContexts.OVERDUE_YQ.equals(loanDetailButtonForm.getWfType(
			 * ))) { if (isPlaceOnFile(loanDetailButtonForm.getLoanapplyNo())) {
			 * buttons.add(ButtonHelper.createAjaxButton("启动归档",
			 * "/archive/rest/startArchive?loanapplyNo=" +
			 * loanDetailButtonForm.getLoanapplyNo(), "placeonfile")); } }
			 */ else if (StatusContexts.O2O_FK_OVER.equals(loanDetailButtonForm.getWfType())) {
				return buttons;
			}
		} else {
			User user = UserHolder.getUser();
			
		      if (StatusContexts.O2O_ZX_BEGIN.equals(loanDetailButtonForm.getWfType()) || StatusContexts.O2O_NEW.equals(loanDetailButtonForm.getWfType())) {
//				if (StringUtils.isNotBlank(o2oloanapply.getOperatorNo()) && o2oloanapply.getOperatorNo().equals(user.getNo())) {
//					buttons.add(ButtonHelper.createAjaxButton("作废", "/rbadloan/cacle?loanapplyNo=" + loanDetailButtonForm.getLoanapplyNo() + "&tType=" + StatusContexts.CANCLE_TYPE));
//				}
				buttons.add(ButtonHelper.createAjaxButton("作废", "/rbadloan/cacle?loanapplyNo=" + loanDetailButtonForm.getLoanapplyNo() + "&tType=" + StatusContexts.CANCLE_TYPE, "loanapplyCancel"));
			} else if (StatusContexts.O2O_ZX_END.equals(loanDetailButtonForm.getWfType())) {
//				if (StringUtils.isNotBlank(o2oloanapply.getOperatorNo()) && o2oloanapply.getOperatorNo().equals(user.getNo())) {
//					buttons.add(ButtonHelper.createAjaxButton("作废", "/rbadloan/cacle?loanapplyNo=" + loanDetailButtonForm.getLoanapplyNo() + "&tType=" + StatusContexts.CANCLE_TYPE));
//				}	
				buttons.add(ButtonHelper.createRedirectButton("运维修改贷款要素", "/oper_up/findloan?loanapplyNo=" + loanDetailButtonForm.getLoanapplyNo() + "&type=detail", "UpdateloanapplyInfoYY"));
				buttons.add(ButtonHelper.createAjaxButton("作废", "/rbadloan/cacle?loanapplyNo=" + loanDetailButtonForm.getLoanapplyNo() + "&tType=" + StatusContexts.CANCLE_TYPE, "loanapplyCancel"));
			} else if (StatusContexts.O2O_NS_BEGIN.equals(loanDetailButtonForm.getWfType())) {
//				if (StringUtils.isNotBlank(o2oloanapply.getOperatorNo()) && o2oloanapply.getOperatorNo().equals(user.getNo())) {
//					buttons.add(ButtonHelper.createAjaxButton("作废", "/rbadloan/cacle?loanapplyNo=" + loanDetailButtonForm.getLoanapplyNo() + "&tType=" + StatusContexts.CANCLE_TYPE));
//				}
				buttons.add(ButtonHelper.createAjaxButton("作废", "/rbadloan/cacle?loanapplyNo=" + loanDetailButtonForm.getLoanapplyNo() + "&tType=" + StatusContexts.CANCLE_TYPE, "loanapplyCancel"));
			} else if (StatusContexts.O2O_NS_END.equals(loanDetailButtonForm.getWfType())) {
				/*
				 * log.info("按钮逻辑-内审完成:isStatus=" +
				 * isStatus(loanDetailButtonForm.getLoanapplyNo())); if
				 * (isStatus(loanDetailButtonForm.getLoanapplyNo())) {
				 * log.info("按钮逻辑-提交保证金与管理费流程"); } // 如果是分期贷接力贷，内审完成不出现面签申报按钮 if
				 * (StatusContexts.LOAN_TYPE_BK.equals(o2oloanapply.getLoanType(
				 * )) ||
				 * StatusContexts.LOAN_TYPE_BP.equals(o2oloanapply.getLoanType()
				 * )) { buttons.add(ButtonHelper.createOpenButton("下载带数据合同",
				 * "/contractDetail?loanapplyNo=" +
				 * loanDetailButtonForm.getLoanapplyNo(), "downloadContract"));
				 * buttons.add(ButtonHelper.createOpenButton("下载O2O金融合作协议",
				 * "/protocolDetail?loanapplyNo=" +
				 * loanDetailButtonForm.getLoanapplyNo(), "downloadProtocol"));
				 * } // 具体的启动逻辑(内审完自动启动的都是直贷流程) if
				 * (o2oloanapply.getLoanType().equals(StatusContexts.
				 * LOAN_TYPE_FQ) ||
				 * o2oloanapply.getLoanType().equals(StatusContexts.LOAN_TYPE_EA
				 * )) { // 先去判断这个单是否已经存在直贷流程 O2oPayEntity o2oPayEntity = new
				 * O2oPayEntity(); o2oPayEntity.setIsdel(StatusContexts.O2O_NO);
				 * o2oPayEntity.setLoanapplyBillcode(loanDetailButtonForm.
				 * getBillCode());
				 * o2oPayEntity.setBillStatus(StatusContexts.WFTYPE_STATUS_NEW);
				 * // 新建状态才能启动流程
				 * o2oPayEntity.setPayType(StatusContexts.PAY_TYPE_ZD); if
				 * (!o2oPayService.isHavePayFlow(o2oPayEntity)) {
				 * buttons.add(ButtonHelper.createAjaxButton("启动付款流程",
				 * "/work/startpay?loanapplyNo=" +
				 * loanDetailButtonForm.getLoanapplyNo() + "&billCode=" +
				 * loanDetailButtonForm.getBillCode() + "&wfType=" +
				 * loanDetailButtonForm.getWfType(), "loanapplyBankReport")); }
				 * }
				 */
//				if (StringUtils.isNotBlank(o2oloanapply.getOperatorNo()) && o2oloanapply.getOperatorNo().equals(user.getNo())) {
//					buttons.add(ButtonHelper.createAjaxButton("作废", "/rbadloan/cacle?loanapplyNo=" + loanDetailButtonForm.getLoanapplyNo() + "&tType=" + StatusContexts.CANCLE_TYPE));
//				}
				buttons.add(ButtonHelper.createAjaxButton("作废", "/rbadloan/cacle?loanapplyNo=" + loanDetailButtonForm.getLoanapplyNo() + "&tType=" + StatusContexts.CANCLE_TYPE, "loanapplyCancel"));
			} else if (StatusContexts.O2O_SB_BEGIN.equals(loanDetailButtonForm.getWfType())) {
				/*
				 * if (isStatus(loanDetailButtonForm.getLoanapplyNo())) {
				 * buttons.add(ButtonHelper.createAjaxButton("提交保证金与管理费流程",
				 * "/work/submitBzj?loanapplyNo=" +
				 * loanDetailButtonForm.getLoanapplyNo() + "&billCode=" +
				 * loanDetailButtonForm.getBillCode() + "&wfType=" +
				 * loanDetailButtonForm.getWfType(), "loanapplyBzj")); }
				 */
				if (StringUtils.isNotBlank(o2oloanapply.getOperatorNo()) && o2oloanapply.getOperatorNo().equals(user.getNo())) {
					buttons.add(ButtonHelper.createAjaxButton("作废", "/rbadloan/cacle?loanapplyNo=" + loanDetailButtonForm.getLoanapplyNo() + "&tType=" + StatusContexts.CANCLE_TYPE));
				}
				buttons.add(ButtonHelper.createAjaxButton("作废", "/rbadloan/cacle?loanapplyNo=" + loanDetailButtonForm.getLoanapplyNo() + "&tType=" + StatusContexts.CANCLE_TYPE, "loanapplyCancel"));
				/*
				 * if ((o2oloanapply.getLoanType().equals(StatusContexts.
				 * LOAN_TYPE_BK) ||
				 * StatusContexts.LOAN_TYPE_BP.equals(o2oloanapply.getLoanType()
				 * )) && o2oloanapply.getBankType().equals(BankRate.BANK_BC)) {
				 * buttons.add(ButtonHelper.createOpenButton("下载带数据合同",
				 * "/contractDetail?loanapplyNo=" +
				 * loanDetailButtonForm.getLoanapplyNo(), "downloadContract"));
				 * }
				 */
			} else if (StatusContexts.O2O_FK_END.equals(loanDetailButtonForm.getWfType())) {
				/*
				 * if (isStatus(loanDetailButtonForm.getLoanapplyNo())) {
				 * buttons.add(ButtonHelper.createAjaxButton("提交保证金与管理费流程",
				 * "/work/submitBzj?loanapplyNo=" +
				 * loanDetailButtonForm.getLoanapplyNo() + "&billCode=" +
				 * loanDetailButtonForm.getBillCode() + "&wfType=" +
				 * loanDetailButtonForm.getWfType(), "loanapplyBzj")); } if
				 * (isPlaceOnFile(loanDetailButtonForm.getLoanapplyNo())) {
				 * buttons.add(ButtonHelper.createAjaxButton("启动归档",
				 * "/archive/rest/startArchive?loanapplyNo=" +
				 * loanDetailButtonForm.getLoanapplyNo(), "placeonfile")); }
				 */
				if (!StatusContexts.LOAN_TYPE_EA.equals(o2oloanapply.getLoanType()) && !StatusContexts.LOAN_TYPE_FQ.equals(o2oloanapply.getLoanType())) {
					buttons.add(ButtonHelper.createAjaxButton("还款完成", "/workflowsubmit/repayment?loanapplyNo=" + loanDetailButtonForm.getLoanapplyNo(), "repaymentComplete"));
				}
				if (isTqhk(loanDetailButtonForm.getLoanapplyNo())) {
					buttons.add(ButtonHelper.createAjaxButton("发起贷款结清", "/loanDetail/advancedPay?loanapplyNo=" + loanDetailButtonForm.getLoanapplyNo(), "advancedRedemption"));
				}
			}
			/*
			 * else if
			 * (StatusContexts.O2O_REPAY_END.equals(loanDetailButtonForm.
			 * getWfType())) { if
			 * (isPlaceOnFile(loanDetailButtonForm.getLoanapplyNo())) {
			 * buttons.add(ButtonHelper.createAjaxButton("启动归档",
			 * "/archive/rest/startArchive?loanapplyNo=" +
			 * loanDetailButtonForm.getLoanapplyNo(), "placeonfile")); } } else
			 * if
			 * (StatusContexts.O2O_FK_DC.equals(loanDetailButtonForm.getWfType()
			 * )) { if (isPlaceOnFile(loanDetailButtonForm.getLoanapplyNo())) {
			 * buttons.add(ButtonHelper.createAjaxButton("启动归档",
			 * "/archive/rest/startArchive?loanapplyNo=" +
			 * loanDetailButtonForm.getLoanapplyNo(), "placeonfile")); } if
			 * (isDc(loanDetailButtonForm.getLoanapplyNo())) {
			 * buttons.add(ButtonHelper.createAjaxButton("启动代偿分期",
			 * "/rback/start?loanApplyNo=" +
			 * loanDetailButtonForm.getLoanapplyNo(), "startBack")); } } else if
			 * (StatusContexts.OVERDUE_YQ.equals(loanDetailButtonForm.getWfType(
			 * ))) { if (isPlaceOnFile(loanDetailButtonForm.getLoanapplyNo())) {
			 * buttons.add(ButtonHelper.createAjaxButton("启动归档",
			 * "/archive/rest/startArchive?loanapplyNo=" +
			 * loanDetailButtonForm.getLoanapplyNo(), "placeonfile")); } }
			 */
			else if (StatusContexts.O2O_FK_OVER.equals(loanDetailButtonForm.getWfType())) {
				return buttons;
			}
		}
		

		buttons.add(ButtonHelper.createOpenButton("补充附件",
					"/payment/otherUpload?referId=" + loanDetailButtonForm.getLoanapplyNo(), "loapplyDetailUpload"));
		
		return buttons;
	}

	/**
	 * 是否缴纳保证金
	 * 
	 * @param loanapplyNo
	 * @return
	 */
	public boolean isStatus(String loanapplyNo) {
		boolean flag = false;
		LoanapplyExpand loanapplyExppand = loanapplyExpandDao.queryLoanapplyNo(loanapplyNo);
		if (loanapplyExppand == null) {
			flag = false;
		} else {
			if (loanapplyExppand.getBillStatus().equals(StatusContexts.BZJ_STATUS_NEW)) {
				flag = true;
			}
		}
		return flag;
	}

	/**
	 * 是否已启动归档流程
	 * 
	 * @param loanapplyNo
	 * @return
	 */
	public boolean isPlaceOnFile(String loanapplyNo) {
		boolean flag = false;
		String sql = "SELECT OL.IS_PLACE_ON_FILE FROM O2O_LOANAPPLY OL WHERE OL.loan_type in ('BK','BP','BP_JX','SG','YC','SC') and OL.bank_loan_name in('BC','GACZ','BBC','QLB','JCB','ZZB','SPDBGZ','ZLB') AND OL.LOANAPPLY_NO = ?";
		String isPlaceOnFile = loanapplyExpandDao.findString(sql, loanapplyNo);
		if (!StringUtils.isEmpty(isPlaceOnFile)) {
			if (StatusContexts.O2O_NEW.equals(isPlaceOnFile)) {
				flag = true;
			}
		}
		return flag;
	}

	/**
	 * @Title: isTqhk
	 * @Description: 提前还款
	 * @author 张庆奇
	 * @param loanapplyNo
	 * @return
	 * @return boolean
	 * @throws @date
	 *             2017年2月21日 下午6:20:08
	 */
	public boolean isTqhk(String loanapplyNo) {
		boolean flag = false;
		AdvancedPayment advancedPayment = advanceSettleService.queryEntity(loanapplyNo, null);
		if (advancedPayment == null) {
			flag = true;
		}
		return flag;
	}

	/**
	 * @Description:
	 * 				<p>
	 *               是否显示启动财务流程按钮
	 *               </p>
	 * @author CXW
	 * @date 2016年12月13日 下午8:43:38
	 * @version V1.0
	 */
	public boolean isStartUpFinance(String loanapplyNo) {
		O2oFinanceFlowEntity entity = financeFlowService.getByLoanApplyno(loanapplyNo);
		if (entity != null) {
			if (entity.getFlowStatus().equals(StatusContexts.O2O_NEW)) {
				return true;
			} else {
				return false;
			}
		} else {
			return true;
		}
	}

	/**
	 * @Description:
	 * 				<p>
	 *               查询是否有代偿分期在流程中，如果有流程中则不显示按钮，如果没有流程中则显示按钮
	 *               </p>
	 * @author CXW
	 * @date 2017年3月9日 下午2:14:51
	 * @version V1.0
	 */
	public boolean isDc(String loanapplyNo) {
		int backEntity = o2oBackService.getCountByLoanApplyNo(loanapplyNo);
		if (backEntity > 0)
			return false;
		else
			return true;
	}
}
